<?php

/**
 * Admin actions.
 *
 * @package    sandbox
 * @subpackage Admin
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 8507 2008-04-17 17:32:20Z fabien $
 */
class codesetAdminActions extends sfActions
{
  public function executeIndex()
  {
    $this->cm_codesetList = CMCodeSet::joinAll();
  }

  public function executeCreate()
  {
    $this->form = new CMCodeSetForm();
    
    $this->setTemplate('edit');
  }

  public function executeEdit($request)
  {
    $this->form = $this->getCMCodeSetForm($request->getParameter('code_set_nm'));
  }

  public function executeUpdate($request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->form = $this->getCMCodeSetForm($request->getParameter('code_set_nm'));

    $this->form->bind($request->getParameter('cm_code_set'));
    if ($this->form->isValid())
    {
      $cm_code_set = $this->form->save();

      $this->redirect('codesetAdmin/edit?code_set_nm='.$cm_code_set['code_set_nm']);
    }

    $this->setTemplate('edit');
  }

  public function executeDelete($request)
  {
    $this->forward404Unless($cm_code_set = $this->getCMCodeSetById($request->getParameter('code_set_nm')));

    $cm_code_set->delete();

    $this->redirect('codesetAdmin/index');
  }
  
  private function getCMCodeSetTable()
  {
    return Doctrine::getTable('CMCodeSet');
  }
  
  private function getCMCodeSetById($id)
  {
    return $this->getCMCodeSetTable()->find($id);
  }
  
  private function getCMCodeSetForm($id)
  {
    $cm_code_set = $this->getCMCodeSetById($id);
    
    if ($cm_code_set instanceof CMCodeSet)
    {
      return new CMCodeSetForm($cm_code_set);
    }
    else
    {
      return new CMCodeSetForm();
    }
  }
}
